package com.csust.automaticteach.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csust.automaticteach.annotation.AuthCheck;
import com.csust.automaticteach.common.BaseResponse;
import com.csust.automaticteach.common.DeleteRequest;
import com.csust.automaticteach.common.ResultUtils;
import com.csust.automaticteach.constant.UserAuthConstant;
import com.csust.automaticteach.exception.ErrorCode;
import com.csust.automaticteach.exception.ThrowUtils;
import com.csust.automaticteach.model.dto.amessage.AmessageAddRequest;
import com.csust.automaticteach.model.dto.amessage.AmessageQueryRequest;
import com.csust.automaticteach.model.dto.amessage.AmessageUpdateRequest;
import com.csust.automaticteach.model.entity.Amessage;
import com.csust.automaticteach.model.entity.User;
import com.csust.automaticteach.model.enums.MessageTypeEnum;
import com.csust.automaticteach.model.vo.amessage.AmessageVO;
import com.csust.automaticteach.service.AmessageService;
import com.csust.automaticteach.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @ClassName AMessageController
 * @Description aMessage 控制器
 * @Author 钰玟
 * @Date 2025/8/20 下午2:54
 * Version 1.0
 **/
@RestController
@RequestMapping("/aMessage")
public class AMessageController {
    @Resource
    private AmessageService aMessageService;
    @Resource
    private UserService userService;

    /**
     * 添加aMessage
     *
     * @param amessageAddRequest 添加aMessage的参数
     * @param request            请求
     * @return 添加的aMessage的id
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    public BaseResponse<Long> addAMessage(@RequestBody AmessageAddRequest amessageAddRequest, HttpServletRequest request) {
        //1.参数校验
        ThrowUtils.throwIf(amessageAddRequest == null, ErrorCode.PARAMS_ERROR);
        MessageTypeEnum enumByValue = MessageTypeEnum.getEnumByValue(amessageAddRequest.getType());
        ThrowUtils.throwIf(enumByValue == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        //2.拷贝参数
        Amessage amessage = new Amessage();
        BeanUtil.copyProperties(amessageAddRequest, amessage);
        amessage.setUserId(loginUser.getId());
        //3.保存
        boolean result = aMessageService.save(amessage);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(amessage.getId());
    }

    /**
     * 删除 aMessage
     *
     * @param deleteRequest 删除参数
     * @return 删除结果
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteAMessage(@RequestBody DeleteRequest deleteRequest) {
        //1.参数校验
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        //2.获取数据
        Long id = deleteRequest.getId();
        Amessage amessage = aMessageService.getById(id);
        ThrowUtils.throwIf(amessage == null, ErrorCode.NOT_FOUND_ERROR);
        //3.删除
        boolean result = aMessageService.removeById(id);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 修改 aMessage
     *
     * @param amessageUpdateRequest 修改参数
     * @return 修改结果
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    public BaseResponse<Long> updateAMessage(@RequestBody AmessageUpdateRequest amessageUpdateRequest) {
        //1.参数校验
        ThrowUtils.throwIf(amessageUpdateRequest == null || amessageUpdateRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        MessageTypeEnum enumByValue = MessageTypeEnum.getEnumByValue(amessageUpdateRequest.getType());
        ThrowUtils.throwIf(enumByValue == null, ErrorCode.PARAMS_ERROR);
        //2.拷贝参数
        Amessage amessage = new Amessage();
        BeanUtil.copyProperties(amessageUpdateRequest, amessage);
        amessage.setUpdateTime(new Date());
        //3.查看数据是否存在
        Amessage oldAmessage = aMessageService.getById(amessage.getId());
        ThrowUtils.throwIf(oldAmessage == null, ErrorCode.NOT_FOUND_ERROR);
        //4.修改数据库
        boolean update = aMessageService.updateById(amessage);
        //5.  返回结果
        ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(amessage.getId());
    }

    @PostMapping("/list/vo/page")
    public BaseResponse<Page<AmessageVO>> listAMessageVOByPage(@RequestBody AmessageQueryRequest amessageQueryRequest) {
        //1.参数校验
        ThrowUtils.throwIf(amessageQueryRequest == null, ErrorCode.PARAMS_ERROR);
        //2. 提取分页参数
        long current = amessageQueryRequest.getCurrent();
        long size = amessageQueryRequest.getPageSize();
        //3. 分页查询
        Page<Amessage> page = aMessageService.page(new Page<>(current, size), aMessageService.getAmessageQueryWrapper(amessageQueryRequest));
        //4. 转换
        Page<AmessageVO> amessageVOPage = new Page<>(current, size,page.getTotal());
        amessageVOPage.setRecords(aMessageService.getAmessageVOList(page.getRecords()));
        //5. 返回
        return ResultUtils.success(amessageVOPage);
    }
}
